const tribonacci = n => {
  if (n === 0) return 0
  if (n === 1 || n === 2) return 1

  let p = 0, q = 0, k = 1, res = 1
  for (let i = 3; i <= n; i++) {
    p = q
    q = k
    k = res
    res = p + q + k
  }
  return res
}

console.log(tribonacci(25));